package com.wanxi.service.impl;

import com.wanxi.dao.NavDao;
import com.wanxi.dao.impl.NavDaoImpl;
import com.wanxi.dto.NavDTO;
import com.wanxi.service.NavService;
import com.wanxi.util.Tool;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class NavServiceImpl implements NavService {
    private NavDao navDao = new NavDaoImpl();

    @Override
    public List<NavDTO> getData() {
        List<NavDTO> navDTOList = new ArrayList<>();
        Jedis jedis = new Jedis();
        boolean isHave = jedis.exists("key");
        if (isHave) {
            List<String> titleList = jedis.lrange("titleList", 0, jedis.llen("titleList"));
            List<String> hrefList = jedis.lrange("hrefList", 0, -1);
            for (int i = 0; i < titleList.size(); i++) {
                NavDTO navDTO = new NavDTO();
                navDTO.setTitle(titleList.get(i));
                navDTO.setHref(hrefList.get(i));
                navDTOList.add(navDTO);
            }
        } else {
            navDTOList = Tool.copy(navDao.getNavModelList(), NavDTO.class);
            for (NavDTO dto : navDTOList
            ) {
                jedis.rpush("titleList", dto.getTitle());
                jedis.rpush("hrefList", dto.getHref());
            }
        }
        return navDTOList;
    }
}
